﻿Ext.define('I2R.controller.HomeController', {
    extend: 'I2R.controller.BaseController',
    views: [
        'news',
        'cases',
        'followups',
        'help',
        'administration',
        'configdepartments',
        'deptformpanel',
        'confignews',
        'newsformpanel'
    ],
    stores: ['phonetypes', 'users', 'departments', 'news'],
    refs: [
        { ref: 'deptformpanel', selector: 'i2rdeptformpanel' },
        { ref: 'departmentsgrid', selector: '#departmentsgrid' },
        { ref: 'newsformpanel', selector: 'i2rnewsformpanel' },
        { ref: 'newsgrid', selector: '#newsgrid' }
    ],
    init: function () {

        var me = this;

        this.control({
            '#header_menu button': {
                click: function (btn, e) {

                    if (btn.action == 'logout') {

                        Ext.MessageBox.confirm('Confirm', 'Are you sure you want to do that?', function (btn) {
                            if (btn == 'yes') {
                                window.location = '../Home/Logout';
                            }
                        });

                        return false;
                    }

                    me.addActionTab(btn.text, btn.action);
                }
            },

            '#administration-tree': {
                itemclick: function (tree, record, item, index, e, eOpts) {
                    me.addActionTabToPanel('administration-tabpanel', record.get('text'), record.get('id'));
                }
            },


            '#departmentsgrid': {
                itemdblclick: function (dv, record, item, index, e) {

                    var id = record.get('Id');

                    var win = new Ext.Window({
                        title: 'New department',
                        id: 'add-department',
                        iconCls: 'icon_building',
                        items: [{ xtype: 'i2rdeptformpanel', skipLoad: false, userEdit: true, loadId: id}],
                        height: 175,
                        width: 325,
                        layout: 'fit',
                        buttons: [
                            { text: 'Save', iconCls: 'icon_disk', action: 'save-department' },
                            { text: 'Cancel', iconCls: 'icon_cancel',
                                handler: function () {
                                    Ext.getCmp('add-department').close();
                                }
                            }
                        ]
                    });

                    win.on('close', function () {
                        me.getDepartmentsgrid().store.load();
                    });

                    win.show();

                }
            },


            '#departmentsgrid button[action=adddepartment]': {
                click: function (btn, e) {

                    var win = new Ext.Window({
                        title: 'New department',
                        id: 'add-department',
                        iconCls: 'icon_building',
                        items: [{ xtype: 'i2rdeptformpanel', skipLoad: true, userEdit: true}],
                        height: 175,
                        width: 325,
                        layout: 'fit',
                        buttons: [
                            { text: 'Save', iconCls: 'icon_disk', action: 'save-department' },
                            { text: 'Cancel', iconCls: 'icon_cancel',
                                handler: function () {
                                    Ext.getCmp('add-department').close();
                                }
                            }
                        ]
                    });

                    win.on('close', function () {
                        me.getDepartmentsgrid().store.load();
                    });

                    win.show();
                }
            },

            '#add-department button[action=save-department]': {
                click: function (btn, e) {

                    var frm = this.getDeptformpanel().getForm();
                    var win = Ext.getCmp('add-department');

                    if (frm.isValid()) {
                        frm.submit({
                            method: 'POST',
                            url: '../User/SaveDepartment',
                            success: function (form, action) {
                                Ext.Msg.alert('Success', 'Successfully saved!');
                                win.close();
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('Failed', action.result.data.errorMessage);
                            }
                        });
                    } else {

                    }
                }
            },


            /* news administration */
            '#newsgrid button[action=addnews]': {
                click: function (btn, e) {


                    var win = new Ext.Window({
                        title: 'Add news or challange',
                        id: 'add-news',
                        iconCls: 'icon_building',
                        items: [{ xtype: 'i2rnewsformpanel', skipLoad: true, userEdit: true}],
                        height: 400,
                        width: 600,
                        layout: 'fit',
                        buttons: [
                            { text: 'Save', iconCls: 'icon_disk', action: 'save-news' },
                            { text: 'Cancel', iconCls: 'icon_cancel',
                                handler: function () {
                                    Ext.getCmp('add-news').close();
                                }
                            }
                        ]
                    });

                    win.on('close', function () {
                        me.getNewsgrid().store.load();
                    });

                    win.show();
                }
            },

            '#add-news button[action=save-news]': {
                click: function (btn, e) {

                    var frm = this.getNewsformpanel().getForm();
                    var win = Ext.getCmp('add-news');

                    if (frm.isValid()) {
                        frm.submit({
                            method: 'POST',
                            url: '../News/Save',
                            success: function (form, action) {
                                Ext.Msg.alert('Success', 'Successfully saved!');
                                win.close();
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('Failed', action.result.data.errorMessage);
                            }
                        });
                    } else {

                    }

                }
            },


            '#newsgrid': {
                itemdblclick: function (dv, record, item, index, e) {

                    var id = record.get('Id');
                    
                    var win = new Ext.Window({
                        title: 'Add news or challange',
                        id: 'add-news',
                        iconCls: 'icon_building',
                        items: [{ xtype: 'i2rnewsformpanel', skipLoad: false, userEdit: true, loadId: id}],
                        height: 400,
                        width: 600,
                        layout: 'fit',
                        buttons: [
                            { text: 'Save', iconCls: 'icon_disk', action: 'save-news' },
                            { text: 'Cancel', iconCls: 'icon_cancel',
                                handler: function () {
                                    Ext.getCmp('add-news').close();
                                }
                            }
                        ]
                    });

                    win.on('close', function () {
                        me.getNewsgrid().store.load();
                    });

                    win.show();

                }
            }

            /* end administration news... */
        });

    },

    addUrlTab: function (tabId, tabTitle, location) {

        var tabPanel = Ext.getCmp('mainTabPanel');
        var tab = Ext.getCmp(tabId);

        if (Ext.isEmpty(tab)) {

            try {

                tab = {
                    id: tabId,
                    xtype: 'panel',
                    title: tabTitle,
                    closable: true,
                    autoScroll: true,
                    layout: 'fit',
                    bodyCls: 'padd-up',
                    items: [
                        {
                            xtype: 'panel',
                            border: false,
                            loader: {
                                autoLoad: true,
                                url: location,
                                scripts: true
                            }
                        }
                    ]
                };

                tabPanel.add(tab).show();
            } catch (err) {
                Ext.Msg.alert('Error!', 'An error occurred: ' + err + '´, this might occur if the view is missing.<br /><br />Help us to report it, to get the product to be as best as possible.');
            }

        } else {
            tabPanel.setActiveTab(tabId);
        }


    },
    addActionTab: function (tabTitle, action) {

        this.addActionTabToPanel('mainTabPanel', tabTitle, action);

        //        var tabPanel = Ext.getCmp('mainTabPanel');
        //        var tabId = "tab-" + action;
        //        var tab = Ext.getCmp(tabId);

        //        if (Ext.isEmpty(tab)) {

        //            try {
        //                var widget = Ext.widget('i2r' + action);

        //                tab = {
        //                    id: tabId,
        //                    xtype: 'panel',
        //                    title: tabTitle,
        //                    items: [widget],
        //                    closable: true,
        //                    autoScroll: true,
        //                    layout: 'fit'
        //                };

        //                tabPanel.add(tab).show();
        //            } catch (err) {
        //                Ext.Msg.alert('Error!', 'An error occurred: ' + err + '´, this might occur if the view is missing.<br /><br />Help us to report it, to get the product to be as best as possible.');
        //            }

        //        } else {
        //            tabPanel.setActiveTab(tabId);
        //        }
    },

    addActionTabToPanel: function (panelName, tabTitle, action) {

        console.log(panelName);

        var tabPanel = Ext.getCmp(panelName);
        var tabId = "tab-" + action;
        var tab = Ext.getCmp(tabId);

        if (Ext.isEmpty(tab)) {
            console.log('i2r' + action);
            try {
                var widget = Ext.widget('i2r' + action);

                tab = {
                    id: tabId,
                    xtype: 'panel',
                    title: tabTitle,
                    items: [widget],
                    closable: true,
                    autoScroll: true,
                    layout: 'fit'
                };

                tabPanel.add(tab).show();
            } catch (err) {
                Ext.Msg.alert('Error!', 'An error occurred: ' + err + '´, this might occur if the view is missing.<br /><br />Help us to report it, to get the product to be as best as possible.');
            }

        } else {
            tabPanel.setActiveTab(tabId);
        }
    }
});